package makovo.controller;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.Map;

import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;


import makovo.utils.DataAccess;

public class Login extends ActionSupport implements SessionAware  {
	private String mUser;
	private String mPassword;
	private Map<String, Object> sessionmap;
	
	public String getUser() {
		return this.mUser;
	}

	public void setUser(String user) {
		this.mUser = user;
	}

	public String getPassword() {
		return this.mPassword;
	}

	public void setPassword(String password) {
		this.mPassword = password;
	}

	public String execute() {
		String accion = "ERROR";
		
		if(validaUsuario()){  
			accion = "SUCCESS";  
	    }  
	    else{  
	    	accion = "ERROR";  
	    }  
		
		return accion;
	}

	private boolean validaUsuario()
	{
		boolean validado = false;
		String cosa="";
		try
		{/* 248643 Reporte */
			System.out.println("Aqui");
			ResultSet rs = DataAccess.getInstance().execResult("iniciar_Sesion(?,?)", new ArrayList<String[]>(){
				{
					add(new String[] { "idUsuario" , mUser });
					add(new String[] { "Password_txt" , mPassword });
				}
			});
			
			if(rs.next()){
				sessionmap.put("LoginNombre",  rs.getString("Nombre"));
				sessionmap.put("login.email", mUser);
				sessionmap.put("login.pass", mPassword);
				sessionmap.put("LogintipoUsr",  rs.getString("idTipoUsr"));
				sessionmap.put("LoginMenu",  rs.getString("URI"));
				validado = true;
			}
		}
		catch (Exception ex){
			System.out.println(ex.getMessage());
		}
		return validado;
	}
	
	@Override
	public void setSession(Map<String, Object> map) {
		 this.sessionmap = map;  
	}
}
